package com.ln.common;

/**
 * 分页计算
 * @author LINING
 */
public final class PagerCompute {

	private static final int PAGE_SIZE = 10;// 默认页面大小

	private int total;// 数据总数
	private int pageNumber;// 分页总数
	private int pageIndex;// 当前页索引
	private int pageFirst;// 数据索引
	private int pageSize;// 分页大小

	private PagerCompute() {
	}

	private PagerCompute init(int pageIndex, int pageSize, int total) {
		if (total == 0)
			return this;

		this.total = total;
		this.pageSize = pageSize < 1 ? PAGE_SIZE : pageSize;
		pageNumber = total / this.pageSize;
		if (total % this.pageSize > 0) {
			pageNumber++;
		}
		if (pageIndex < 1) {
			pageIndex = 1;
		}
		if (pageIndex > pageNumber) {
			pageIndex = pageNumber;
		}

		this.pageIndex = pageIndex;
		this.pageFirst = (pageIndex - 1) * this.pageSize;
		if (this.pageFirst > 0)
			this.pageFirst--;

		return this;
	}

	public static PagerCompute build(int pageIndex, int pageSize, int total) {
		return new PagerCompute().init(pageIndex, pageSize, total);
	}

	public int getTotal() {
		return total;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public int getPageFirst() {
		return pageFirst;
	}

	public int getPageSize() {
		return pageSize;
	}
}